package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix;

import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.VisitableElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

import java.util.ArrayList;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/*********************************************************
 * 文件名称：SuffixSelectByExampleWithoutBLOBsElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix
 * 功能说明：带后缀不带Blob的条件查询xml生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/30 13:55
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixSelectByExampleWithoutBLOBsElementGenerator extends
        AbstractXmlElementGenerator {


    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
        answer.addAttribute(new Attribute("id", //$NON-NLS-1$
                SuffixUtil.getSuffixMethodName(introspectedTable.getSelectByExampleStatementId(),introspectedTable)));
        answer.addAttribute(new Attribute(
                "resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
        answer.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$

        context.getCommentGenerator().addComment(answer);
        List<VisitableElement> elements = new ArrayList<>();
        TextElement select = new TextElement("select");
        elements.add(select);
        XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
        ifElement.addAttribute(new Attribute("test", "example != null and example.distinct"));
        ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
        elements.add(ifElement);
        StringBuilder sb = new StringBuilder();
        TextElement getByExampleQueryId = null;
        if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,"); //$NON-NLS-1$
            getByExampleQueryId = new TextElement(sb.toString());
            elements.add(getByExampleQueryId);
        }
        elements.add(getBaseColumnListElement());

        sb.setLength(0);
        sb.append("from "); //$NON-NLS-1$
        sb.append(SuffixUtil.addTableNameSuffix(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime(),introspectedTable));
        elements.add(new TextElement(sb.toString()));
        elements.add(getUpdateByExampleIncludeElement());

        VisitableElement foreachSuffix = SuffixUtil.getForeachSuffix(elements.toArray(new VisitableElement[0]));
        answer.addElement(select);
        answer.addElement(ifElement);
        if (getByExampleQueryId !=null){
            answer.addElement(getByExampleQueryId);
        }
        answer.addElement(getBaseColumnListElement());
        answer.addElement(new TextElement("from ("));
        answer.addElement(foreachSuffix);
        answer.addElement(new TextElement(")"));
        ifElement = new XmlElement("if"); //$NON-NLS-1$
        ifElement.addAttribute(new Attribute("test", "example != null and example.orderByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$
        ifElement.addElement(new TextElement("order by ${example.orderByClause}")); //$NON-NLS-1$
        answer.addElement(ifElement);

        if (context.getPlugins().sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            SuffixUtil.deleteOldElement(parentElement,answer);
            parentElement.addElement(answer);
        }
    }
}
